<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
use App\Http\Controllers\Controller;

class PhotoController extends Controller
{
    public $path;

    public function __construct()
    {
        $this->path = 'attachs/img/';
    }
    /**
     * 上传图片
     * @return [type] [description]
     */
    public function upload()
    {
        $data = Input::all();
        $photo = $data['file'];

        //判断文件是否有效
        if (!$photo->isValid()) {
            return Response::json([
                'status' => 'error',
                'message' => '无效文件',
            ], 200);
        }

        $name = md5(time().$photo->getClientOriginalName().rand(1,999));
        $ext = '.'.$photo->getClientOriginalExtension();
        $this->uniqueFilename($this->path, $name, $ext);

        $photo->move($this->path, $name . $ext);
        $img = Image::make($this->path . $name . $ext);
        $img->resize(300,200)->save($this->path.$name.'_300x200'.$ext);

        if(!$img) {
            return Response::json([
                'status' => 'error',
                'message' => '上传失败',
            ], 200);
        }

        return Response::json([
            'status'  =>  'success',
            'message' => '上传成功',
            'url'   =>  $this->path . $name.'_300x200' . $ext,
        ], 200);
    }

    /**
     * 裁剪图片
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function crop()
    {
        dd(Input::all());
        $form_data = Input::all();
        $image = $form_data['file'];
        $name = md5(time().$image->getClientOriginalName().rand(1,999));
        $ext = '.'.$image->getClientOriginalExtension();
        $this->uniqueFilename($this->path, $name, $ext);

        // resized sizes
        $imgW = $form_data['imgW'];
        $imgH = $form_data['imgH'];
        // offsets
        $imgY1 = $form_data['imgY1'];
        $imgX1 = $form_data['imgX1'];
        // crop box
        $cropW = $form_data['width'];
        $cropH = $form_data['height'];
        // rotation angle
        $angle = $form_data['rotation'];

        $image = Image::make($this->path . $name . $ext);
        $image->resize($imgW, $imgH)
            ->rotate(-$angle)
            ->crop($cropW, $cropH, $imgX1, $imgY1)
            ->save($this->path . $name . $ext);

        if( !$image) {
            return Response::json([
                'status' => 'error',
                'message' => '上传失败',
            ], 200);
        }

        return Response::json([
            'status' => 'success',
            'message' => '上传成功',
            'url' => $this->path . $name . $ext,
        ], 200);
    }

    /**
     * 生成唯一的文件名
     * @param  [type] $path  [description]
     * @param  [type] &$name [description]
     * @param  [type] $ext   [description]
     * @return [type]        [description]
     */
    private function uniqueFilename($path, &$name, $ext)
    {
        if ( File::exists( $path.$name.$ext ) )
        {
            var_dump($name);
            $name = sha1($name.time());
            $this->uniqueFilename($path, $name, $ext);
            dd($name);
        }
    }
}
